//
//  AddCartOrBuyView.m
//  com.modada.new
//
//  Created by ferver7 on 2019/5/7.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "AddCartOrBuyView.h"
#import "AddKindItemCell.h"
#import "TwoSelKindItem.h"
//#import "BuyOrderViewController.h"
#import "LZCartPayViewController.h"

@implementation AddCartOrBuyView
- (IBAction)closeAction:(id)sender {
    [GKCover hide];
}
- (IBAction)addCart:(id)sender {
    if (self.totalSelNum == 0) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"未添加任何产品"];
        return;
    }else if (self.totalSelNum < [self.goodItem.batchNum integerValue]){
        [AxcUI_Toast AxcUI_showCenterWithText:@"添加商品小于起批数"];
        return;
    }
    
    NSMutableArray * listArr = [[NSMutableArray alloc]init];
    for (NSDictionary * infoObj in self.selModuleItemArr) {
        NSMutableArray * kinds = infoObj[@"list"];
        for (TwoSelKindItem * kindObj in kinds) {
            NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
            setDickeyobj(parameters, kindObj.gaid, @"gaid");
            NSString * selNum = [NSString stringWithFormat:@"%ld",kindObj.tapNum];
            setDickeyobj(parameters, selNum, @"quantity");
            [listArr addObject:parameters];
        }
    }
    NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
    setDickeyobj(requestInfo, listArr, @"list");
    [PPNetworkHelper POST:app_shoppingCart_insert parameters:requestInfo success:^(id responseObject) {
          if (YJPPSUCCESS(responseObject)) {
              [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"msg"]];
              [GKCover hide];
          }else{
              [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"msg"]];
          }
    } failure:^(NSError *error) {
    
    } showLoading:nil];
}
- (IBAction)buyAction:(id)sender {
    [GKCover hide];
    if (self.totalSelNum == 0) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"未选择任何产品"];
        return;
    }else if (self.totalSelNum < [self.goodItem.batchNum integerValue]){
        [AxcUI_Toast AxcUI_showCenterWithText:@"选中商品小于起批数"];
        return;
    }
//    for (NSDictionary * infoObj in self.selModuleItemArr) {
//        NSMutableArray * kinds = infoObj[@"list"];
//        if (kinds.count == 0) {
//            [self.selModuleItemArr removeObject:infoObj];
//        }
//    }
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    NSMutableArray * listArr = [[NSMutableArray alloc]init];
    for (NSDictionary * goodInfo in self.selModuleItemArr) {
        NSArray * goodItems = goodInfo[@"list"];
        for (TwoSelKindItem *object in goodItems) {
            NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
            setDickeyobj(parameters, object.gaid, @"gaid");
            NSString * selNum = [NSString stringWithFormat:@"%ld",object.tapNum];
            setDickeyobj(parameters, selNum, @"quantity");
            [listArr addObject:parameters];
        }
    }
    setDickeyobj(parameters, listArr, @"goods");
    setDickeyobj(parameters, @"-1", @"addressId");
    
    
    [PPNetworkHelper POST:app_shoppingCart_shop parameters:parameters success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            LZCartPayViewController * lzcp = mGetStoryboardViewController(@"Main", @"lZCartPayViewController");
            lzcp.payInfo = responseObject[@"data"];
            [self.pushRoot.navigationController pushViewController:lzcp animated:YES];
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        
    } showLoading:nil];
    
//    BuyOrderViewController * bovc = mGetStoryboardViewController(@"Main", @"buyOrderViewController");
//    bovc.selModuleItemArr = self.selModuleItemArr;
//    bovc.totalMomey = self.totalMoney;
//    bovc.goodItem = self.goodItem;
//    [self.pushRoot.navigationController pushViewController:bovc animated:YES];
}
-(void)awakeFromNib
{
    [super awakeFromNib];
    self.textArray = [[NSMutableArray alloc]init];
    self.moduleItemArr = [[NSMutableArray alloc]init];
    self.selModuleItemArr = [[NSMutableArray alloc]init];
    [self addSubview:self.tagTextView]; //商品属性个数控制。
    self.selfTableView.delegate = self;
    self.selfTableView.dataSource = self;
    self.selfTableView.tableFooterView = [UIView new];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.moduleItemArr.count > 0) {
        NSMutableDictionary * infoObj = self.moduleItemArr[self.tagSel];
        NSMutableArray * kinds = infoObj[@"list"];
        return kinds.count+1;
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        UITableViewCell * headerCell = [[NSBundle mainBundle]loadNibNamed:@"AddCartOrBuyView" owner:nil options:nil][1];
        return headerCell;
    }else{
        NSMutableDictionary * infoObj = self.moduleItemArr[self.tagSel];
        NSMutableArray * kinds = infoObj[@"list"];
        TwoSelKindItem * objItem = kinds[indexPath.row -1];
        AddKindItemCell * itemCell = [[NSBundle mainBundle]loadNibNamed:@"AddCartOrBuyView" owner:nil options:nil].lastObject;
        itemCell.nameLabel.text = objItem.attName;
        itemCell.kuCunLabel.text = objItem.stock;
        itemCell.priceLabel.text = objItem.price;
        itemCell.numLabel.text = [NSString stringWithFormat:@"%ld",objItem.tapNum];
       
        if ([itemCell.numLabel.text isEqualToString:@"0"]) {
            [itemCell.subBtn setBackgroundColor:[UIColor lightGrayColor]];
        }else{
            [itemCell.subBtn setBackgroundColor:KColor(0, 122, 255)];
        }
        
        itemCell.returnAction = ^(NSInteger type) {
            if (type == 0) {//减1
                if (objItem.tapNum == 0) {
                    [AxcUI_Toast AxcUI_showCenterWithText:@"没有添加"];
                    return ;
                }
                objItem.tapNum--;
                self.totalSelNum --;
                self.totalMoney -= objItem.price.floatValue;
                self.totalNumLabel.text = [NSString stringWithFormat:@"总数量:%ld",self.totalSelNum];
                self.totalMoneyLabel.text = [NSString stringWithFormat:@"总金额:%.2lf",self.totalMoney];
                if (objItem.tapNum == 0) {
                    NSMutableDictionary * selInfoObj = self.selModuleItemArr[self.tagSel];
                    NSMutableArray * selKinds = selInfoObj[@"list"];
                    if ([selKinds containsObject:objItem]) {
                        [selKinds removeObject:objItem];
                    }else{
                        
                    }
                }
                itemCell.numLabel.text = [NSString stringWithFormat:@"%ld",objItem.tapNum];
            }else{//加1
                if (objItem.tapNum >= objItem.stock.integerValue) {
                    [AxcUI_Toast AxcUI_showCenterWithText:@"库存不足"];
                    return ;
                }
                objItem.tapNum++;
                self.totalSelNum ++;
                self.totalMoney += objItem.price.floatValue;
                self.totalNumLabel.text = [NSString stringWithFormat:@"总数量:%ld",self.totalSelNum];
                self.totalMoneyLabel.text = [NSString stringWithFormat:@"总金额:%.2lf",self.totalMoney];
                NSMutableDictionary * selInfoObj = self.selModuleItemArr[self.tagSel];
                NSMutableArray * selKinds = selInfoObj[@"list"];
                if ([selKinds containsObject:objItem]) {
                    
                }else{
                    [selKinds addObject:objItem];
                }
                itemCell.numLabel.text = [NSString stringWithFormat:@"%ld",objItem.tapNum];
            }
        };
        return itemCell;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 44;
    }
    return 45;
}
-(void)setGoodItem:(AppBaseGoodItem *)goodItem
{
    _goodItem = goodItem;
    
    self.nameLabel.text = goodItem.name;
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:goodItem.thumbnail] placeholderImage:mImageByName(@"")];
    if (goodItem.lowerPrice) {
        self.priceLabel.text = [NSString stringWithFormat:@"¥%@起",goodItem.lowerPrice];
    }else{
        self.priceLabel.text = [NSString stringWithFormat:@"¥%@起",@"0"];
    }
    self.sellerLabel.text = [NSString stringWithFormat:@"已售%@",goodItem.salesVolume];
    if (goodItem.batchNum) {
        self.qiPiLabel.text = [NSString stringWithFormat:@"%@组起批",goodItem.batchNum];
    }else{
        self.qiPiLabel.text = [NSString stringWithFormat:@"%@组起批",@"0"];
    }
    self.shengyuLabel.text = [NSString stringWithFormat:@"剩余%ld",goodItem.totalStock.integerValue ];
    [self getAllGoodTypeWith:goodItem];
}

-(void)getAllGoodTypeWith:(AppBaseGoodItem *)goodItem
{
    NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
    setDickeyobj(requestInfo, goodItem.gid, @"gid");
    setDickeyobj(requestInfo, @"1", @"add");
    
    [PPNetworkHelper POST:app_mall_goodTypeAtt parameters:requestInfo success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            self.goodItem.batchNum =responseObject[@"data"][@"batchNum"];
            self.qiPiLabel.text = [NSString stringWithFormat:@"%@组起批",self.goodItem.batchNum];
            
            for (NSDictionary * infoDci in responseObject[@"data"][@"typeList"]) {
                [self.textArray addObject:infoDci[@"typeName"]];
                
                NSMutableDictionary * infoObj = [[NSMutableDictionary alloc]init];
                setDickeyobj(infoObj, infoDci[@"gtid"], @"gtid");
                setDickeyobj(infoObj, infoDci[@"typeName"], @"typeName");
                
                NSMutableDictionary * selinfoObj = [[NSMutableDictionary alloc]init];
                setDickeyobj(selinfoObj, infoDci[@"gtid"], @"gtid");
                setDickeyobj(selinfoObj, infoDci[@"typeName"], @"typeName");
                
                NSMutableArray * twoKindArr =[[NSMutableArray alloc]init];
                for (NSDictionary * twoKindObj in infoDci[@"list"]) {
                    TwoSelKindItem * itemModel = [[TwoSelKindItem alloc]init];
                    [itemModel setValuesForKeysWithDictionary:twoKindObj];
                    [twoKindArr addObject:itemModel];
                }
                setDickeyobj(infoObj, twoKindArr, @"list");
                [self.moduleItemArr addObject:infoObj];
                setDickeyobj(selinfoObj, [[NSMutableArray alloc]init], @"list");
                [self.selModuleItemArr addObject:selinfoObj];
            }
            [self.tagTextView AxcUI_addTags:self.textArray];
            [self.tagTextView AxcUI_setTagAtIndex:0 selected:YES];
            self.tagSel = 0;
            [self.tagTextView AxcUI_reloadData];
            [self.selfTableView reloadData];
        }else{
            
        }
    } failure:^(NSError *error) {
        
    } showLoading:nil];
}
#pragma mark - 懒加载区
- (AxcUI_TagTextView *)tagTextView{
    if (!_tagTextView) {
        _tagTextView = [[AxcUI_TagTextView alloc] init];
        _tagTextView.axcUI_Size = CGSizeMake(mScreenWidth - 30, 35);
        _tagTextView.axcUI_X = 15;
        _tagTextView.axcUI_Y = 152;
        
        AxcTagTextConfig * config = [[AxcTagTextConfig alloc]init];
        config.axcUI_tagTextFont = mTitleFont(13);
        config.axcUI_tagTextColor = [UIColor AxcUI_colorWithHexCode:@"#676767"];
        config.axcUI_tagSelectedTextColor = [UIColor whiteColor];
        config.axcUI_tagBackgroundColor = [UIColor clearColor];
        config.axcUI_tagSelectedBackgroundColor = [UIColor AxcUI_colorWithHexColor:@"#6933DB"];
        config.axcUI_tagCornerRadius = 10;
        config.axcUI_tagSelectedCornerRadius = 10;
        
        config.axcUI_tagExtraSpace = CGSizeMake(20, 7);
        
        _tagTextView.axcUI_defaultConfig= config;
        _tagTextView.axcUI_tagTextViewDelegate = self;
        _tagTextView.axcUI_scrollDirection = AxcTagViewScrollDirectionStyleHorizontal;
        _tagTextView.backgroundColor = [UIColor whiteColor];
    }
    return _tagTextView;
}
- (void)AxcUI_tagTextView:(AxcUI_TagTextView *)tagTextView
                didTapTag:(NSString *)tagText
                  atIndex:(NSUInteger)index
                 selected:(BOOL)selected;
{
    if (self.tagSel == index) {
        [self.tagTextView AxcUI_setTagAtIndex:self.tagSel selected:YES];
    }else{
        [self.tagTextView AxcUI_setTagAtIndex:self.tagSel selected:NO];
        self.tagSel = index;
        [self.tagTextView AxcUI_setTagAtIndex:self.tagSel selected:YES];
        [self.selfTableView reloadData];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
